using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class SlidingDoorItem : BaseItem
	{
		public enum State
		{
			Left = 0,
			Right = 1
		}

		public enum Anim
		{
			Static = 0,
			MoveRight = 1,
			MoveLeft = 2
		}

		private const float RIGHT_POSITION = 3.74f;

		public State state;

		public Anim anim;

		private float animTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			state = (State)data.state;
			if (state == State.Right)
			{
				model.SetLocalX(3.74f);
			}
			Transform trans = base.transform.Find("Mask");
			trans.SetLocalZ(-1E-05f);
		}

		protected override void LateUpdate()
		{
			if (anim != 0)
			{
				animTimer += Time.deltaTime;
				if (anim == Anim.MoveRight)
				{
					model.SetLocalX(Mathf.Lerp(0f, 3.74f, animTimer));
				}
				else
				{
					model.SetLocalX(Mathf.Lerp(3.74f, 0f, animTimer));
				}
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim == Anim.Static)
			{
				if (state == State.Left)
				{
					anim = Anim.MoveRight;
					state = State.Right;
				}
				else
				{
					anim = Anim.MoveLeft;
					state = State.Left;
				}
				Singleton<AudioManager>.Instance.PlaySound("sound_sliding_door");
				animTimer = 0f;
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
